<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Origami::Filter::RunLength</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Origami::Filter::RunLength</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/origami/filters/runlength_rb.html">
                origami/filters/runlength.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Class representing a <a href="../Filter.html">Filter</a> used to <a
href="RunLength.html#M000360">encode</a> and <a
href="RunLength.html#M000361">decode</a> data using RLE compression
algorithm.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000361">decode</a>&nbsp;&nbsp;
      <a href="#M000360">encode</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../Filter.html">Filter</a></span>
      </div>
    </div>

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000361" class="method-detail">
        <a name="M000361"></a>

        <div class="method-heading">
          <a href="#M000361" class="method-signature">
          <span class="method-name">decode</span><span class="method-args">(stream)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Decodes data using RLE decompression method.
</p>
<table>
<tr><td valign="top"><em>stream</em>:</td><td>The data to <a href="RunLength.html#M000361">decode</a>.

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000361-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000361-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters/runlength.rb, line 93</span>
 93:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">decode</span>(<span class="ruby-identifier">stream</span>)
 94:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">InvalidRunLengthDataError</span>, <span class="ruby-value str">&quot;No end marker&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-constant">EOD</span>.<span class="ruby-identifier">chr</span>)
 95:         
 96:         <span class="ruby-identifier">i</span> = <span class="ruby-value">0</span>
 97:         <span class="ruby-identifier">result</span> = <span class="ruby-value str">&quot;&quot;</span>
 98:         <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span>].<span class="ruby-identifier">ord</span> <span class="ruby-operator">==</span> <span class="ruby-constant">EOD</span> <span class="ruby-keyword kw">do</span>
 99:         
100:           <span class="ruby-identifier">length</span> = <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span>]
101:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">EOD</span>
102:             <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>, <span class="ruby-identifier">length</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>]
103:             <span class="ruby-identifier">i</span> = <span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">+</span> <span class="ruby-value">2</span>
104:           <span class="ruby-keyword kw">else</span>
105:             <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>,<span class="ruby-value">1</span>] <span class="ruby-operator">*</span> (<span class="ruby-value">257</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">length</span>)
106:             <span class="ruby-identifier">i</span> = <span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-value">2</span>
107:           <span class="ruby-keyword kw">end</span>
108:           
109:         <span class="ruby-keyword kw">end</span>
110:         
111:         <span class="ruby-identifier">result</span>
112:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000360" class="method-detail">
        <a name="M000360"></a>

        <div class="method-heading">
          <a href="#M000360" class="method-signature">
          <span class="method-name">encode</span><span class="method-args">(stream)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Encodes data using RLE compression method.
</p>
<table>
<tr><td valign="top"><em>stream</em>:</td><td>The data to <a href="RunLength.html#M000360">encode</a>.

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000360-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000360-source">
<pre>
    <span class="ruby-comment cmt"># File origami/filters/runlength.rb, line 45</span>
45:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">encode</span>(<span class="ruby-identifier">stream</span>)
46: 
47:         <span class="ruby-identifier">result</span> = <span class="ruby-value str">&quot;&quot;</span>
48:         <span class="ruby-identifier">i</span> = <span class="ruby-value">0</span>
49: 
50:         <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">size</span>
51:           
52:           <span class="ruby-comment cmt">#</span>
53:           <span class="ruby-comment cmt"># How many identical bytes coming?</span>
54:           <span class="ruby-comment cmt">#</span>
55:           <span class="ruby-identifier">length</span> = <span class="ruby-value">1</span>
56:           <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">size</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">EOD</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>]
57:             <span class="ruby-identifier">length</span> = <span class="ruby-identifier">length</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
58:             <span class="ruby-identifier">i</span> = <span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
59:           <span class="ruby-keyword kw">end</span>
60: 
61:           <span class="ruby-comment cmt">#</span>
62:           <span class="ruby-comment cmt"># If more than 1, then compress them.</span>
63:           <span class="ruby-comment cmt">#</span>
64:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span>
65:             <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-value">257</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">length</span>).<span class="ruby-identifier">chr</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span>,<span class="ruby-value">1</span>]
66: 
67:           <span class="ruby-comment cmt">#</span>
68:           <span class="ruby-comment cmt"># Otherwise how many different bytes to copy ?</span>
69:           <span class="ruby-comment cmt">#</span>
70:           <span class="ruby-keyword kw">else</span>
71:             <span class="ruby-identifier">j</span> = <span class="ruby-identifier">i</span>
72:             <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">j</span><span class="ruby-operator">+</span><span class="ruby-value">1</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">size</span> <span class="ruby-keyword kw">and</span> (<span class="ruby-identifier">j</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>) <span class="ruby-operator">&lt;</span> <span class="ruby-constant">EOD</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">j</span>] <span class="ruby-operator">!=</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">j</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>]
73:               <span class="ruby-identifier">j</span> = <span class="ruby-identifier">j</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
74:             <span class="ruby-keyword kw">end</span>
75: 
76:             <span class="ruby-identifier">length</span> = <span class="ruby-identifier">j</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">i</span>
77:             <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">length</span>.<span class="ruby-identifier">chr</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">stream</span>[<span class="ruby-identifier">i</span>, <span class="ruby-identifier">length</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>]
78: 
79:             <span class="ruby-identifier">i</span> = <span class="ruby-identifier">j</span>
80:           <span class="ruby-keyword kw">end</span>
81: 
82:           <span class="ruby-identifier">i</span> = <span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
83:         <span class="ruby-keyword kw">end</span>
84: 
85:         <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">EOD</span>.<span class="ruby-identifier">chr</span>
86:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>